package com.cloud.pole.msg.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.ace.cache.api.CacheAPI;
import com.cloud.pole.api.msg.MsgNoticeApi;
import com.cloud.pole.common.constant.CacheConstant;
import com.cloud.pole.common.constant.CommonConstants;
import com.cloud.pole.common.exception.BaseException;
import com.cloud.pole.common.exception.ParamException;
import com.cloud.pole.common.msg.ApiRestResponse;
import com.cloud.pole.common.util.RegexUtils;
import com.cloud.pole.msg.model.entity.MsgNotice;
import com.cloud.pole.msg.service.MsgNoticeService;
import com.cloud.pole.msg.service.SmsSendService;
import com.cloud.pole.msg.service.VerifyCodeService;
import com.github.pagehelper.PageInfo;

import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping("/notice/front")
@Slf4j
public class NoticeController {
	
	@Autowired
    private MsgNoticeService msgNoticeService;

	@RequestMapping(value = "/getUserNoticeList")
    @ResponseBody
    public ApiRestResponse<List<MsgNotice>> getUserNoticeList(
    		@RequestParam(defaultValue = "1") String userType,
    		@RequestHeader(defaultValue = "") String token,
    		@RequestParam(defaultValue = "") String pageSize,
    		@RequestParam(defaultValue = "") String pageNum) throws Exception {
		try {
			ParamException.err(StringUtils.isBlank(token), "token不能为空");
			ParamException.err(StringUtils.isBlank(pageSize), "pageSize");
			ParamException.err(StringUtils.isBlank(pageNum), "pageNum不能为空");
			ParamException.err(StringUtils.isBlank(pageNum), "pageNum错误");
			ParamException.err(!RegexUtils.isPositiveInteger(pageNum), "pageNum错误");
			PageInfo<MsgNotice> pageInfo = msgNoticeService.getPageUserNoticeList(token, Integer.parseInt(userType), Integer.parseInt(pageSize), Integer.parseInt(pageNum));
			return ApiRestResponse.successOfPage(CommonConstants.STATUS_OK_MESSAGE, (int)pageInfo.getPages(), pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getList());
		} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
	}
	
	
	@RequestMapping(value = "/getNotice")
    @ResponseBody
    public ApiRestResponse<MsgNotice> getNotice(
    		@RequestHeader(defaultValue = "") String token,
    		@RequestParam(defaultValue = "") String noticeId) throws Exception {
		try {
			ParamException.err(StringUtils.isBlank(noticeId), "noticeId错误");
			ParamException.err(!RegexUtils.isPositiveInteger(noticeId), "noticeId错误");
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, msgNoticeService.getNotice(token, Integer.parseInt(noticeId)));
		} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
	}
}
